<div class="container">
	 <div class="panel panel-primary starter-template">
    <div class="panel-heading"> Version </div>
    <div class="panel-body">
      <p> Here's the version of our project. </p>
      <p>
      	Current version: <?php echo $version; ?>
      	<a data-toggle="modal" data-target="#add">
      		<span class="glyphicon glyphicon-star-empty"> </span>
      	</a>
      </p>
    </div>

    <table id="query_table" class="table table-hover table-striped">
      <script type="text/javascript">
        $(function(){
          $('#query_table td:nth-child(3)').css('text-align','center').siblings().css('text-align','center')
        })
      </script>

      <thead>
        <tr>
          <th width="10%"> # </th>
          <th width="10%"> Virsion name </th>
          <th width="50%"> Version Description </th>
          <th width="20%"> Last Update </th>
          <th width="10%"> Edit </th>
        </tr>
      </thead>
      <tbody>

      	<?php
      		foreach ($allVersion->result() as $row) {
      	?>
      		<tr>
          		<td> <?php echo $row->ID; ?> </td>
          		<td>
          			<a data-toggle="modal" data-target="#show" onclick="show('<?php echo $row->ID;?>')">
          				<?php echo $row->name; ?>
          			</a>
          		</td>
          		<td> <?php echo $row->description; ?> </td>
         		<td> <?php echo $row->last_update; ?> </td>
         		<td> 
         			<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#edit"
         				onclick="edit('<?php echo $row->ID;?>', '<?php echo $row->name;?>', '<?php echo $row->description;?>')">
         				<span class="glyphicon glyphicon-edit"> </span>
         			</button>
         		</td>
        	</tr>
      	<?php
      		}
      	?>
      </tbody>
    </table>
  </div>
</div>


<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel"> Add version </h4>
      </div>
      <div class="modal-body">

			<form class="form-horizontal" role="form" action="<?php echo site_url('/test/add_version'); ?>" method="post">
			  <div class="form-group">
			    <label for="versionNameAdd" class="col-sm-4 control-label"> Version name </label>
			    <div class="col-sm-8">
			      <input type="text" class="form-control" id="versionNameAdd" name = "name" placeholder="input version name">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="versionDescriptionAdd" class="col-sm-4 control-label"> Version description </label>
			    <div class="col-sm-8">
			      <input type="text" class="form-control" id="versionDescriptionAdd" placeholder="input version description" name="description">
			    </div>
			  </div>
			  <div class="form-group">
			    <div class="col-sm-offset-4 col-sm-8">
			      <div class="checkbox">
			        <label>
			          <input type="checkbox" name="check"> Set as current version
			        </label>
			      </div>
			    </div>
			  </div>
			  <div class="form-group">
			  	<div class="col-sm-offset-10 col-sm-2">
			  		<button type="submit" class="btn btn-primary">Create</button>
			  	</div>
			  </div>
			</form>

      </div>
    </div>
  </div>
</div>


<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel"> Edit the version </h4>
      </div>
      <div class="modal-body">

			<form class="form-horizontal" role="form" action="<?php echo site_url('/test/edit_version'); ?>" method="post">
			  <div class="form-group">
			    <label for="versionID" class="col-sm-4 control-label"> Version ID </label>
			    <div class="col-sm-8">
			      <input type="text" class="form-control" id="versionID" name = "ID" placeholder="input version ID" readonly>
			    </div>
			  </div>

			  <div class="form-group">
			    <label for="versionName" class="col-sm-4 control-label"> Version name </label>
			    <div class="col-sm-8">
			      <input type="text" class="form-control" id="versionName" name = "name" placeholder="input version name">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="versionDescription" class="col-sm-4 control-label"> Version description </label>
			    <div class="col-sm-8">
			      <input type="text" class="form-control" id="versionDescription" placeholder="input version description" name="description">
			    </div>
			  </div>
			  <div class="form-group">
			    <div class="col-sm-offset-4 col-sm-8">
			      <div class="checkbox">
			        <label>
			          <input type="checkbox" name="check"> Set as current version
			        </label>
			      </div>
			    </div>
			  </div>
			  <div class="form-group">
			  	<div class="col-sm-offset-10 col-sm-2">
			  		<button type="submit" class="btn btn-primary"> Save </button>
			  	</div>
			  </div>
			</form>

      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
	function edit(id, name, description) {
		$("#versionID").val(id);
		$("#versionName").val(name);
		$("#versionDescription").val(description);
	}
</script>


<div class="modal fade" id="show" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel"> Show the version </h4>
      </div>
      <div class="modal-body" id="test_result">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
	function show(id) {
		var data = id;
		$.get('<?php echo site_url("/test/get_result"); ?>', {id: data}, function(res){
			$("#test_result").html(res);
		})
	}
</script>
